package cn.edu.nuc.onlinestore.frame;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.onlinestore.model.Admin;
import cn.edu.nuc.onlinestore.network.TCPServer;
import demo.factor.PeoFactory.PeoFactory;

public class AdminLogin extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private JButton button;
	private JFrame frame = null;
	
	
	

	//静态块，一开始写入管理员的账号和密码，自动创建目录
	static{
		AdminName.onlinenumber=new HashSet<>();
		File f=new File("D:/store/admin/");
		if(!f.exists())
		{
			f.mkdirs();
		}
		try
		(
			ObjectOutputStream out1=new ObjectOutputStream(new FileOutputStream("D:/store/admin/Admin.dat"));
				){
			Admin admin=new Admin();
			admin.setNumber("0001");
		
			admin.setPassword("666");
			//得到用户号
			AdminName.name=admin.getNumber();
			out1.writeObject(admin);
		}catch(Exception e)
		{}		
				
	}
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminLogin frame = new AdminLogin();
					frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
					frame.setVisible(true);
					//启动监听
					new TCPServer().start();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Launch the application.
	 */


	

	/**
	 * Create the frame.
	 */
	public AdminLogin() {
		this.frame=this;
		setTitle("中北线在商场管理系统-管理员登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("用户名:");
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("密  码:");
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);
		
		textField = new JTextField();
		textField.setBounds(151, 146, 197, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(151, 185, 197, 21);
		contentPane.add(passwordField);
		
		//确认按钮
		button= new JButton("登录系统");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//从文件获得admin
				Admin admin=PeoFactory.fa.readAdmin();
				System.out.println(admin);
				//判断账号和密码
				if(getTextField().getText().equals(admin.getNumber())&&new String(getPasswordField().getPassword()).equals(admin.getPassword()))
				{
					//进入管理员界面
					AdminStore st=new AdminStore();
					st.setAdminlogin(frame);
					//关闭本窗口
					frame.dispose();
					//打开AdminStore窗口
					st.setVisible(true);
				}else{
					JOptionPane.showMessageDialog(null,"用户名和密码错误,或找不到这个管理员","", JOptionPane.NO_OPTION);
				}	
			}
		});
		
		
		button.setBounds(255, 216, 93, 23);
		contentPane.add(button);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(AdminLogin.class.getResource("/image/a.jpg")));
		lblNewLabel.setBounds(208, 54, 86, 82);
		contentPane.add(lblNewLabel);
	}

	public JTextField getTextField() {
		return textField;
	}

	public void setTextField(JTextField textField) {
		this.textField = textField;
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	public void setPasswordField(JPasswordField passwordField) {
		this.passwordField = passwordField;
	}

	public JButton getButton() {
		return button;
	}

	public void setButton(JButton button) {
		this.button = button;
	}
}
